﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HaiFang
{
    public partial class PageControl : System.Web.UI.UserControl, IPageControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public int CurrentPage
        {
            get
            {
                if (ViewState["CurrentPage"] == null)
                    ViewState["CurrentPage"] = 1;

                return Convert.ToInt32(ViewState["CurrentPage"]);

            }
            set
            {
                if (ViewState["CurrentPage"] == null || ViewState["CurrentPage"].ToString() != value.ToString())
                {
                    ViewState["CurrentPage"] = value;
                    if (this.OnCurrentPageChange != null)
                    {
                        this.OnCurrentPageChange((int)(value));
                    }

                    HideButton();
                }
            }
        }

        public int TotalRows
        {
            get
            {
                if (ViewState["TotalRows"] == null)
                    ViewState["TotalRows"] = 0;

                return Convert.ToInt32(ViewState["TotalRows"]);
            }
            set
            {
                ViewState["TotalRows"] = value;

                this.HideButton();
            }
        }

        private int m_PageSize = 10;
        public int PageSize
        {
            get
            {
                return m_PageSize;
            }
            set
            {
                m_PageSize = value;
            }
        }

        public event Action<int> OnCurrentPageChange;

        protected void btFirst_Click(object sender, EventArgs e)
        {
            this.CurrentPage = 1;

        }
        protected void btPre_Click(object sender, EventArgs e)
        {
            this.CurrentPage--;
        }
        protected void btNext_Click(object sender, EventArgs e)
        {
            this.CurrentPage++;
        }

        protected void btLast_Click(object sender, EventArgs e)
        {
            this.CurrentPage = (int)(TotalRows / PageSize)+1;
        }

        protected void btGo_Click(object sender, EventArgs e)
        {
            int nMaxPageCount = (int)(this.TotalRows / this.PageSize) + ((TotalRows % this.PageSize) > 0 ? 1 : 0);

            int nPageNum = this.CurrentPage;

            try
            {
                nPageNum = Convert.ToInt32(this.txtPageNum.Text);
            }
            catch
            {

            }

            if (nPageNum > nMaxPageCount)
            {
                this.txtPageNum.Text = nMaxPageCount.ToString();

                nPageNum = nMaxPageCount;
            }

            if (nPageNum < 1)
                nPageNum = 1;

            this.CurrentPage = nPageNum;

        }

        private void HideButton()
        {
            int nMaxPageCount = (int)(this.TotalRows / this.PageSize) + ((TotalRows % this.PageSize) > 0 ? 1 : 0);

            this.btPre.Enabled = true;
            this.btNext.Enabled = true;
            this.btFirst.Enabled = true;
            this.btLast.Enabled = true;
            this.btGo.Enabled = true;

            if (this.CurrentPage == 1)
            {
                this.btPre.Enabled = false;
                this.btFirst.Enabled = false;

                if (nMaxPageCount <= 0)
                {
                    this.btNext.Enabled = false;
                    this.btLast.Enabled = false;
                    this.btGo.Enabled = false;
                }
            }
            else if (this.CurrentPage >= nMaxPageCount)
            {
                this.btNext.Enabled = false;
                this.btLast.Enabled = false;
            }


            if (this.TotalRows == 0)
            {
                this.lbPage.Text = "";
            }
            else
            {
                this.lbPage.Text = string.Format("第{0}页，共{1}页，共计{2}条数据", this.CurrentPage, nMaxPageCount, this.TotalRows);
            }
        }
    }
}
